<!-- https://segmentfault.com/a/1190000021289438 -->
<template>
  <div class="mavonEditor">
    <el-input placeholder="请输入标题" v-model="context.title" :style="inputSty">
      <el-select v-model="context.catalog" slot="prepend" placeholder="文章分类" :style="inputTarget">
        <el-option :label="item.articleCatalog" :value="item.id" v-for="item in this.catalog" :key="item.id"></el-option>
      </el-select>
      <template slot="append">
        <el-button type="danger" round :style="release" @click="releaseArticle()">发布文章</el-button>
      </template>
    </el-input>


      <!-- 封面图上传 -->
      <div style="text-align: center;">
        <el-upload
          class="upload-demo"
          drag
          :limit="1"
          :action="BASE_API+'/fileUpload'"
          list-type="picture"
          name="picture"
          :on-success="handleSuccess"
          :on-exceed="onExceed"
          :on-remove="handleRemove"
          >
         <i class="el-icon-upload"></i>
          <div class="el-upload__text"><em>点击或拖入</em>上传封面图</div>
        </el-upload>
      </div>
      <!-- tinymce编辑器 -->
      <!-- <mce-editor
          :config           = "Config"
          v-model          = "Value"
          url              = "http://blogapi.fengbooks.club/fileUpload"
          :max-size         = "MaxSize"
          :accept           = "Accept"
          :with-credentials = false
          @on-ready         = "onEditorReady"
          @on-destroy       = "onEditorDestroy"
          @on-upload-success= "onEditorUploadComplete"
          @on-upload-fail   = "onEditorUploadFail"
        ></mce-editor> -->
     <mce-editor
       v-model             = "value"
       :url                 = "BASE_API+'/fileUpload'"
       @input              = "onEditorReady"
     ></mce-editor>

     <div style="border-left: 1px solid #EBEEF5;">
       <!-- 标签添加 -->
       <div class="article-target">
        <label>文章标签:</label>
        <el-tag
          :key="index"
          v-for="(tag,index) in context.labels"
          closable
          :type="tag.type"
          effect="dark"
          :disable-transitions="false"
          @close="handleClose(index)">
          {{tag.articleLabel}}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model="inputValue"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        >
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 添加标签</el-button>
        <label style="color:＃C0C4CC;font-size: 12px;">*最多5个标签哦</label>
       </div>

       <!-- 文章类型 -->
       <div class="article-target">
         <label>文章类型:</label>
         <el-select v-model="context.type" clearable placeholder="请选择" size="small">
           <el-option
             v-for="item in options"
             :key="item.value"
             :label="item.label"
             :value="item.value">
           </el-option>
         </el-select>
       </div>

       <!-- 发布形式 -->
       <div class="article-target" style="display: flex;justify-content: flex-start;align-items: center;">
         <label>发布形式:&nbsp;</label>
         <el-checkbox-group
           v-model="context.form"
           :max="1">
           <el-checkbox v-for="(form ,index) in forms" :label="form" :key="index">{{form}}</el-checkbox>
         </el-checkbox-group>
       </div>
     </div>

  </div>
</template>

<script>
  import Upload from '@/api/user'
  import ArticleApi from '@/api/Article'
  import mixins from '@/mixins.js'
  import MceEditor from '@/views/common/ckEditor'
  const cityOptions = ['公开', '私密'];
  export default {
    mixins:[mixins],
    data(){
      return{
        // 接口API地址
        BASE_API: process.env.BASE_API,
        inputSty:{
          fontSize:16+'px',
          height:40+'px',
        },
        inputTarget:{
          width:120+'px',
          color:'white',
          backgroundColor:'#409EFF',
        },
        release:{
          backgroundColor:'#f55134',
          color:'white',
          borderRadius:0+'px'
        },
        value:'',
        // 文章提交数据
        context:{
          title:"",
          value:"",
          uid:"",
          coverImg:'',
          catalog:'',
          // 标签
          labels: [
             // { type: '', label: '' },
          ],
          type:'',
          form: [],
        },
        // 接口API地址
        BASE_API: process.env.BASE_API,
        // 封面图
        formdata : new FormData(),

        inputVisible: false,
        inputValue: '',
        tagType:['','success','info','danger','warning'],
        // 文章分类
        catalog:[],
        // 文章类型
        options: [{
                  value: '原创',
                  label: '原创'
                }, {
                  value: '转载',
                  label: '转载'
                }],
        // 发布形式
        forms: cityOptions
      }
    },
    components:{
      MceEditor
    },
    methods: {
      releaseArticle(){
        if(this.context.value === null || this.context.value === ""){
          this.$notify({
            title: '温馨提示',
            message: '请为文章添加内容！！！',
            type: 'warning',
            // position: 'top-left',
            offset: 100,
            duration: 2000
          });
        }else if(this.context.title === null || this.context.title === ""){
          this.$notify({
            title: '温馨提示',
            message: '请输入文章标题！！！',
            type: 'warning',
            // position: 'top-left',
            offset: 100,
            duration: 2000
          });
        }else if(this.context.coverImg === null || this.context.coverImg === ""){
          this.$notify({
            title: '温馨提示',
            message: '请上传封面图！！！',
            type: 'warning',
            // position: 'top-left',
            offset: 100,
            duration: 2000
          });
        }else if(this.context.catalog === null || this.context.catalog === ''){
          this.$notify({
            title: '温馨提示',
            message: '请为文章选择分类哦！！！',
            type: 'warning',
            // position: 'top-left',
            offset: 100,
            duration: 2000
          });
        }else if(this.context.type === null || this.context.type === ''){
          this.$notify({
            title: '温馨提示',
            message: '请为文章选择类型哦！！！',
            type: 'warning',
            // position: 'top-left',
            offset: 100,
            duration: 2000
          });
        }else if(this.context.labels.length === 0 ){
            this.$notify({
              title: '温馨提示',
              message: '请给文章添加标签',
              type: 'warning',
              // position: 'top-left',
              offset: 100,
              duration: 2000
            });
        }else if(this.context.form.length === 0 ){
          this.$notify({
            title: '温馨提示',
            message: '请选择发布类型！！！',
            type: 'warning',
            // position: 'top-left',
            offset: 100,
            duration: 2000
          });
        }else{
        Upload.releaseArticle(this.context).then(res => {
          if(res.data.isSuccess){
            this.$message({
                type: 'success',
                message: '发布成功！',
                duration: 1000,
                showClose: true,
                center: true
            });

            // 清空内容
            this.context.title = ""
            this.context.value = ""
          }
        })
      }
      },
      //文件上传成功的钩子函数
        handleSuccess(res, file){
            this.$message({
                type: 'success',
                message: '图片上传成功',
                duration: 1000,
                showClose: true,
                center: true
            });
            this.context.coverImg=file.response.imgUrl
        },
        //删除文件之前的钩子函数
        handleRemove(file) {
            this.$message({
                type: 'success',
                message: '已删除所选图片',
                duration: 1000,
                showClose: true,
                center: true
            });
        },
        //点击列表中已上传的文件事的钩子函数
        handlePreview(file) {
          this.dialogImageUrl = file.url;
          this.dialogVisible = true;
        },
        //上传的文件个数超出设定时触发的函数
        onExceed(files) {
            this.$message({
                type: 'error',
                message: '封面图只能为一张',
                duration: 1000,
                showClose: true,
                center: true
            });
        },

        // 标签选择

        // 删除标签
        handleClose(tag) {
          this.context.labels.splice(tag, 1);
        },

        showInput() {
          this.inputVisible = true;
          this.$nextTick(_ => {
            this.$refs.saveTagInput.$refs.input.focus();
          });
        },

        handleInputConfirm() {
          // 判断标签数是不是大于3个
          if(this.context.labels.length === 5){
            this.$message({
                type: 'error',
                message: '标签最多添加5个哦',
                duration: 1000,
                showClose: true,
                center: true
            });
            // 清空输入框的
            this.inputVisible = false;
            this.inputValue = '';
          }else{
            // 随机生成标签主题色
            // var type =Math.floor(Math.random()*(this.tagType.length-1)+0);
            var type = this.context.labels.length
            let inputValue = this.inputValue;
            if (inputValue) {
              // alert(this.tagType[type])
              // 随机添加不同颜色的tag 随机生成下标 根据下标 选择颜色tagType[type]
              this.context.labels.push({type:this.tagType[type],articleLabel:inputValue});
            }
            this.inputVisible = false;
            this.inputValue = '';
          }
        },
        onEditorReady(res){
          // console.log(res)
          this.context.value = res
        }
    },
    created() {
      // 获取用户id
      this.context.uid = this.UserInfo.id;
      // 获取文章分类
      ArticleApi.getCatalogList().then(res => {
        this.catalog = res.data
      })
    }
  }
</script>

<style scoped="scoped">
  /* 发布文章，上传封面 */
  .el-upload-dragger{
    width: 100%;
    border: none;
  }
  .el-upload-list--picture .el-upload-list__item-thumbnail{
    width: 150px;
  }
  .el-select .el-input {
    width: 130px;
  }


/* 标签 */
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    /* margin-left: 10px; */
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

  .article-target {
    margin: 20px 50px
  }
  .article-target label{
    padding-right: 10px;
  }
  .mce-content-body {
    height: 500px;
  }
</style>
